package window;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dao.TeacherDao;

import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Deletepage {
	String id="";
	public JFrame deletepageFrame;
	private JTextField textField;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the application.
	 */
	public Deletepage(String id) {
		this.id=id;
		initialize();
		
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		deletepageFrame = new JFrame();
		deletepageFrame.setBounds(-7, 0, 1380, 736);
		deletepageFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		deletepageFrame.getContentPane().setLayout(null);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 1364, 23);
		deletepageFrame.getContentPane().add(menuBar);
		
		JMenu mnNewMenu = new JMenu("操作(ALT+1)");
		mnNewMenu.setHorizontalAlignment(SwingConstants.CENTER);
		mnNewMenu.setFont(new Font("幼圆", Font.PLAIN, 15));
		mnNewMenu.setForeground(Color.DARK_GRAY);
		mnNewMenu.setMnemonic('1');
		menuBar.add(mnNewMenu);
		
		JMenuItem menuItem = new JMenuItem("返回(F)");
		menuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				TeacherHomepage th=new TeacherHomepage(id);
				th.teacherHomepageFrame.setVisible(true);
				deletepageFrame.dispose();
			}
		});
		menuItem.setFont(new Font("幼圆", Font.PLAIN, 13));
		menuItem.setForeground(Color.DARK_GRAY);
		menuItem.setMnemonic('F');
		mnNewMenu.add(menuItem);
		
		JMenuItem mntme = new JMenuItem("退出登录(E)");
		mntme.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Login lg=new Login();
				lg.LoginFrame.setVisible(true);
				deletepageFrame.dispose();
			}
		});
		mntme.setMnemonic('E');
		mntme.setForeground(Color.DARK_GRAY);
		mntme.setFont(new Font("幼圆", Font.PLAIN, 13));
		mnNewMenu.add(mntme);
		
		JLabel lblNewLabel_1 = new JLabel("欢迎你，亲爱的");
		lblNewLabel_1.setForeground(Color.DARK_GRAY);
		lblNewLabel_1.setFont(new Font("幼圆", Font.PLAIN, 12));
		lblNewLabel_1.setBounds(10, 37, 90, 15);
		deletepageFrame.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("New label");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel_2.setText(id);
		lblNewLabel_2.setForeground(Color.DARK_GRAY);
		lblNewLabel_2.setFont(new Font("幼圆", Font.BOLD, 13));
		lblNewLabel_2.setBounds(98, 36, 90, 15);
		deletepageFrame.getContentPane().add(lblNewLabel_2);
		
		JLabel label = new JLabel("工号：");
		label.setForeground(Color.DARK_GRAY);
		label.setFont(new Font("幼圆", Font.BOLD, 15));
		label.setBounds(540, 140, 80, 43);
		deletepageFrame.getContentPane().add(label);
		
		
		JButton button = new JButton("提交");
		
		textField = new JTextField();
		textField.setDocument(new MyDocument(11));
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
					button.doClick();
			}
		});
		textField.setColumns(10);
		textField.setBounds(626, 151, 165, 21);
		deletepageFrame.getContentPane().add(textField);
		
		JLabel label_8 = new JLabel("教师信息删除");
		label_8.setHorizontalAlignment(SwingConstants.CENTER);
		label_8.setForeground(Color.DARK_GRAY);
		label_8.setFont(new Font("宋体", Font.BOLD, 30));
		label_8.setBounds(0, 50, 1364, 31);
		deletepageFrame.getContentPane().add(label_8);
	
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(textField.getText().length()>0) {
					if(TeacherDao.checkId(textField.getText())) {
						/*System.out.print(textField.getText());*/
						TeacherDao.delete(textField.getText());	
						JOptionPane.showMessageDialog(null, "成功删除！", "success！", JOptionPane.PLAIN_MESSAGE);
						Timer timer = new Timer();
					        timer.schedule(new TimerTask() {  
					            public void run() {  
					            	TeacherHomepage th=new TeacherHomepage(id);
									th.teacherHomepageFrame.setVisible(true);
									deletepageFrame.dispose();
					            }  
					        }, 500);	
					}
					else JOptionPane.showMessageDialog(null, "工号不存在！", "error！", JOptionPane.ERROR_MESSAGE);	
				}
				else JOptionPane.showMessageDialog(null, "请输入工号！", "error！", JOptionPane.ERROR_MESSAGE);
			}
		});
		button.setForeground(Color.DARK_GRAY);
		button.setFont(new Font("幼圆", Font.BOLD, 16));
		button.setBounds(620, 580, 130, 35);
		deletepageFrame.getContentPane().add(button);
		
		JLabel label_1 = new JLabel("，请输入要删除的教师信息工号！");
		label_1.setForeground(Color.DARK_GRAY);
		label_1.setFont(new Font("幼圆", Font.PLAIN, 12));
		label_1.setBounds(198, 37, 205, 15);
		deletepageFrame.getContentPane().add(label_1);
		deletepageFrame.setTitle("教师信息管理系统");
		ImageIcon icon=new ImageIcon("D:\\eclipse\\workspace\\TeacherInformationManagementSystem\\src\\window\\1.png");
		deletepageFrame.setIconImage(icon.getImage());
		
	}
}
